package com.ljx.NoteBody.service.impl;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.google.code.kaptcha.Constants;
import com.ljx.NoteBody.common.MyException;
import com.ljx.NoteBody.dto.UserDto;
import com.ljx.NoteBody.entity.Note;
import com.ljx.NoteBody.entity.User;
import com.ljx.NoteBody.mapper.UserMapper;
import com.ljx.NoteBody.service.UserService;
import com.ljx.NoteBody.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Value("${Image.URL}")
    String URL;

    @Override
    public void register(UserDto userDto, HttpServletRequest httpServletRequest) {
        String answer = (String) httpServletRequest.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        String myCode = userDto.getCode();
        if(!myCode.equals(answer)){
            throw new MyException("验证码错误");
        }
        LocalDateTime time = LocalDateTime.now();
        if(userMapper.queryUserByUsername(userDto.getUsername()) != null){
            throw new MyException("该账号已经存在");
        }
        userDto.setNickname("HelloWorld");
        userDto.setCreateTime(time);
        userDto.setUpdateTime(time);
        userDto.setAvatar(URL + "image/ikun.jpg");
        int i = userMapper.saveUser(userDto);
        if(i <= 0){
            throw new MyException("保存用户出错");
        }
    }

    @Override
    public Map<String, Object> login(User user, HttpServletRequest httpServletRequest,
                                     HttpServletResponse httpServletResponse) {
        User getUser = userMapper.queryUserByUsernameAndPassword(user.getUsername(), user.getPassword());
        if(getUser == null){
            throw new MyException("账号或者密码错误");
        }

        // 设置返回用户信息
        getUser.loginRemoveItem();

        // 设置返回对象
        String token = TokenUtil.getToken(getUser);
        Map<String, Object> map = new HashMap();
        map.put("userInfo", getUser);
        map.put("token", token);

        // username存放session
        httpServletRequest.getSession().setAttribute("username", getUser.getUsername());
        return map;
    }

    @Override
    public void logout(HttpServletRequest httpServletRequest) {
        try{
            httpServletRequest.getSession().removeAttribute("username");
        } catch (Exception e){
            throw new MyException("用户退出错误");
        }
    }

    @Override
    public User queryUserByUsername(String username, HttpServletRequest httpServletRequest) {
        return userMapper.queryUserByUsername(username);
    }


    @Override
    public User updateUserInfo(User user) {
        int i = userMapper.updateUserInfo(user);
        if(i <= 0) throw new MyException("用户更新信息出错");
        return userMapper.queryUserByUsername(user.getUsername());
    }


}
